package admin

import (
	"invitation/internal/application"
	"invitation/internal/pojo/vo"

	"github.com/gofiber/fiber/v2"
)

func GameListAllEndpoint(application *application.Application) func(ctx *fiber.Ctx) error {
	return func(ctx *fiber.Ctx) error {
		gameInfos, err := application.RepoManager().GameInfoRepo().FindListAll()
		if err != nil {
			return ctx.Status(500).JSON(fiber.Map{
				"message": err.Error(),
			})
		}
		var dataToReturn []vo.Game
		for _, gameInfo := range gameInfos {
			gameIntroduction, err := application.RepoManager().GameIntroductionRepo().FindOneByGameInfoID(gameInfo.ID)
			if err != nil {
				return ctx.Status(500).JSON(fiber.Map{
					"message": err.Error(),
				})
			}
			gameTheory, err := application.RepoManager().GameTheoryRepo().FindOneByGameInfoID(gameInfo.ID)
			if err != nil {
				return ctx.Status(500).JSON(fiber.Map{
					"message": err.Error(),
				})
			}
			dataToReturn = append(dataToReturn, vo.Game{
				GameInfo:          gameInfo,
				Introduction:      gameIntroduction.Introduction,
				TheoryExplanation: gameTheory.TheoryExplanation,
			})
		}
		return ctx.Status(200).JSON(dataToReturn)
	}

}
